<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/19
 * Time: 13:54
 */

namespace app\mec\model;


use think\Model;

class Classroom extends Model
{
    /**
     * 查询课程信息
     * @param $where
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     */
    public function getInfo($where, $field = '*'){
        $info = $this->where($where)->field($field)->find();
        return $info;
    }
    
    /**
     * 获取课程列表
     * @param $field
     * @param $page
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function classroomLists($where, $field, $page, $limit){
        $lists = $this->alias('uc')
            ->join('up_classroom_type uct', 'uct.classroom_id = uc.classroom_id', 'left')
            ->join('up_mechanism um', 'um.mechanism_id = uc.mechanism_id', 'left')
            ->field($field)
            ->where($where)
            ->page($page, $limit)
            ->order('uc.id desc')
            ->select();
        return $lists;
    }
    
    /**
     * 统计总记录数，用于分页
     * @return int|string
     */
    public function getCount($where){
        $count = $this->alias('uc')
            ->join('up_classroom_type uct', 'uct.classroom_id = uc.id', 'left')
            ->join('up_mechanism um', 'um.mechanism_id = uc.mechanism_id', 'left')
            ->where($where)
            ->count();
        return $count;
    }
    
    /**
     * 创建课程
     * @param $data
     * @return false|int
     */
    public function insertClassroom($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 审核、查看审核
     * @param $classroom_id
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     */
    public function classroomAudit($classroom_id, $field){
        $info = $this->alias('uc')
            ->join('up_classroom_type uct', 'uct.classroom_id = uc.classroom_id', 'left')
            ->where('id', $classroom_id)
            ->field($field)
            ->find();
        return $info;
    }
    
    /**
     * 更新记录
     * @param $classroom_id
     * @param $data
     * @return $this
     */
    public function updateClassroom($classroom_id, $data){
        $res = $this->where('id', $classroom_id)->update($data);
        return $res;
    }
}